/*
 * Created by Weiting on 2011/11/11
 * copyright 2011 III. All rights reserved.
 *
 */
package com.eslserver.action.dropdown;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.eslserver.dao.CoordinatorDAO;
import com.eslserver.dao.DAOException;
import com.eslserver.dao.ESLServerDAOFactory;
import com.eslserver.to.Coordinator;
import com.eslserver.util.JSONResponseUtil;

@SuppressWarnings("serial")
public class CoordinatorDropdownServlet extends HttpServlet {
	public static final Logger LOG = LoggerFactory
			.getLogger(CoordinatorDropdownServlet.class);

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		PrintWriter writer = resp.getWriter();
		try {
			CoordinatorDAO coorDAO = ESLServerDAOFactory.getDAOFactory(
					ESLServerDAOFactory.ESLSERVER_DAOFACTORY)
					.getCoordinatorDAO();

			Map<Integer, String> coorMap = new HashMap<Integer, String>();

			for (Coordinator coor : coorDAO.loadAllCoordinators()) {

				coorMap.put(coor.getCoordinatorID(), coor.getName());
			}

			writer.println(JSONResponseUtil.getJSONObjectString(coorMap));

		} catch (DAOException e) {
			LOG.error("error laoding Coordinator", e);
		}
	}

}
